package com.power.algorithm.sort;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/5/21 11:41 下午
 */
public class SelectionSort {
    static int[] array5 = Tools.createOrigin5Array();
    static int[] array100 = Tools.createOrigin100Array();

    public static void main(String[] args) {
        SelectionSort selectionSort = new SelectionSort();
        int[] arr = selectionSort.sort(array5);
        Tools.display(arr);
        int[] arr2 = selectionSort.sort(array100);
        Tools.display(arr2);
    }

    /**
     * 1. 取最小的值  如果  min > array[i]
     * 2. 记录最小的值   以及索引的位置
     * 3. 遍历到最后  然后交换i的位置
     */

    public int[] sort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int min = Integer.MAX_VALUE;
            int index = -1;

            for (int j = i; j < array.length; j++) {
                if (min > array[j]) {
                    min = array[j];
                   // System.out.println(min);
                    index = j;
                   // System.out.println(index);
                }
            }
            int tmp = array[index];
            array[index] = array[i];
            array[i] = tmp;
        }
        return array;
    }
}
